<?php
class View_Message extends View_Base  {
	/**
	 * (non-PHPdoc)
	 * @see View_Base::showOneInList()
	 */
	public function showOneInAdminList($arrMessage) {
		Service_Debug::setDebug();
			echo '<tr class ="messages';
			if (!$arrMessage['is_read_at'] && $arrMessage['ID_user_to'] == User_User::getId()) echo ' not_read';
			echo '" interlocutor="' . $arrMessage['ID_user'] . '">';
			echo'<td class="mail_from_photo">';
					echo '<a href="' . W_ABSPATH . $arrMessage['user_type'] . '/' . $arrMessage['ID_user'] . '">';
						$filename = IMAGES . 'essence/user/' . $arrMessage['ID_user'] . '/s_profile' . '.jpg';
						if(file_exists($filename)) echo '<img src="' . W_IMAGES . 'essence/user/' . $arrMessage['ID_user'] . '/s_profile' . '.jpg">';
						else echo '<img src="' . W_IMAGES . 'essence/user/s_nophoto.gif">';
					echo '</a>';
			echo '</td>';
			echo '<td>';
				echo '<div class="name">
					<a href="' . W_ABSPATH . $arrMessage['user_type'] . '/' . $arrMessage['ID_user'] . '">' .  $arrMessage['first_name'] . ' ' .  $arrMessage['last_name'] . '</a>
				</div>';
				echo '<div class="date">' .  Service_Date::changeDateFormat($arrMessage['created_at'], 'Y-m-d H-i-s', 'j R Y в H:i') . '</div>';
			echo '</td>';
		
			echo '<td class="mail_content">';
				echo '<a href=".' . W_ABSPATH . 'messages?user=' . $arrMessage['ID_user'] . '" class="mail_links mail_short_text">';
					echo Service_Function::utf8_string_with_limit($arrMessage['text'], 60);
				echo '</a>';
			echo '</td>';
			echo '</tr>';
	}
	
	/**
	 * Метод отображает список элементов переданного массива
	 *
	 * @param array $arrMessages - массив сообщений для отображения
	 * @param int $pagesNumber - количество страниц для навигации
	 * @param int $pageNumber - номер текущей страницы
	 */
	public function showAdminList($arrMessages,$pagesNumber,$pageNumber) {
		Service_Debug::setDebug();
		echo '<div id="message-history">';
			echo '<table id="all-last-messages">';
			if(!$arrMessages) echo 'Сообщений нет.';
			else foreach ($arrMessages as $arrMessage) $this->showOneInAdminList($arrMessage);
			echo '</table>';		
		echo '</div>';
		if ($pagesNumber > 1) {
			$pattern =  W_ABSPATH . 'messages?';
			$this->getNavigation($pattern, $pageNumber, $pagesNumber);
		}
	}
	
	/**
	 * Выводит страницу переписки пользователя с конкретным собеседником
	 *
	 * @param array $arrMessages - массив последних сообщений
	 * @param int $reciever_Id - id собеседника
	 */
	public function showChat ($arrMessages,$objUser,$pagesNumber,$pageNumber) {
		Service_Debug::setDebug();
		$objForm = new Form_ViewForm(array(
				'errorIfExists' => true), new Message);
		echo '<div id="envelope">';
			echo '<div id="left-envelope">';
			echo '<div id="middle-user-photo">';
				echo '<a href="' . W_ABSPATH . $objUser->getField('user_type') . '/' . $objUser->id . '">';
				$filename = IMAGES . 'essence/user/' . $objUser->id . '/m_profile' . '.jpg';
				if(file_exists($filename)) echo '<img src="' . W_IMAGES . 'essence/user/' . $objUser->id . '/m_profile' . '.jpg">';
				else echo '<img src="' . W_IMAGES . 'essence/user/m_nophoto.gif">';
				echo '</a>';
			echo '</div>';
			echo '<h1>' . $objUser->getField('first_name') . ' ' . $objUser->getField('last_name') . '</h1>';
			echo '</div>';
			$objForm->show();
			$objForm->textareaField(array('cols' => 3, 'rows' => 10, 'text' => '','wideTextarea' => true), 'text');
			$objForm->hiddenField(array('value' => User_User::getId()),'ID_user');// id текущего пользователя
			$objForm->hiddenField(array('value' => $objUser->id),'ID_user_to');// id получателя в случае отправки сообщения
			$objForm->submitButton(array('value' => 'Отправить'), 'save');
			$objForm->close();
		echo '</div>';
		
		if($arrMessages){
			echo '<div id="message-history">';
				echo '<table>';
				foreach ($arrMessages as $arrMessage)
				{
					echo '<tr class ="messages';
						if (!$arrMessage['is_read_at'] && $arrMessage['ID_user_to'] == User_User::getId()) echo ' new';
					echo '" id="' . $arrMessage['ID_message'] . '">';
						echo'<td class="mail_from">'; 
							echo '<div class="name">';
								if (SimpleAdministration::isAuthor($arrMessage['ID_user'])) echo '<a href="' . W_ABSPATH . 'profile">' . $arrMessage ['first_name'] . '</a>';
								else echo '<a href="' . W_ABSPATH . $arrMessage['user_type'] . '/' . $arrMessage['ID_user'] .  '">' . $arrMessage ['first_name'] . '</a>';
							echo '</div>';
							echo '<div class="date">' . Service_Date::changeDateFormat($arrMessage['created_at'], 'Y-m-d H-i-s', 'j R Y в H:i') . '</div>';
						echo '</td>';
					
						echo '<td class="mail_content">';
						echo '<div class="mail_short_text">' . $arrMessage['text'] . '</div>';
						echo '</td>';
						echo '<td class="message-options">';
							echo '<a href="' . W_ABSPATH . 'messages?del=' . $arrMessage['ID_message'] . '">Удалить</a>';
						echo '</td>';
					echo '</tr>';
				}
				echo '</table>';
			echo '</div>';
		}
		if ($pagesNumber > 1) {
			$pattern =  W_ABSPATH . 'messages?user=' . $objUser->id . '&';
			$this->getNavigation($pattern, $pageNumber, $pagesNumber);
		}
	}
}
?>